package com.liantong.util;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JsonUtil {

	/**
	 * 从一个JSON 对象字符格式中得到一个java对象，形如： {"id" : idValue, "name" : nameValue,
	 * "aBean" : {"aBeanId" : aBeanIdValue, ...}}
	 * 
	 * @param object
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static synchronized Object getDTO(String source, Class clazz) {
		Object bean = null;
		ObjectMapper mapper = null;
		mapper = new ObjectMapper();
		try {
			String formatPattern = "yyyy-MM-dd HH:mm:ss";
			DateFormat dateFormat = new SimpleDateFormat(formatPattern);

			mapper.setDateFormat(dateFormat);
			bean = mapper.readValue(source, clazz);

			// mapper.configure(SerializationConfig.Feature.USE_STATIC_TYPING,
			// true);
			// mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
			// false);
			// mapper.configure(DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES,
			// false);
			// mapper.configure(SerializationConfig.Feature., false);
			mapper.getSerializationConfig().setDateFormat(dateFormat);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bean;
	}

	@SuppressWarnings("unchecked")
	public static synchronized Object getObjectFromJson(String source,
			Class clazz) {
		Object bean = null;
		try {
			ObjectMapper mapper = new ObjectMapper();
			bean = mapper.readValue(source, clazz);
			String formatPattern = "yyyy-MM-dd";
			DateFormat dateFormat = new SimpleDateFormat(formatPattern);

			mapper.setDateFormat(dateFormat);
			// mapper.configure(SerializationConfig.Feature.USE_STATIC_TYPING,
			// true);
			 mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
			 false);
			 mapper.configure(DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES,
			 false);
			// mapper.configure(SerializationConfig.Feature., false);
			mapper.getSerializationConfig().setDateFormat(dateFormat);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bean;
	}

	/**
	 * 把数据对象转换成json字符串 DTO对象形如：{"id" : idValue, "name" : nameValue, ...}
	 * 数组对象形如：[{}, {}, {}, ...] map对象形如：{key1 : {"id" : idValue, "name" :
	 * nameValue, ...}, key2 : {}, ...}
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonGenerationException
	 */
	public static synchronized String getJSONString(Object object) {
		String jsonString = null;
		ObjectMapper mapper = new ObjectMapper();
		try {
			jsonString = mapper.writeValueAsString(object);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return jsonString == null ? "{}" : jsonString;
	}

	public static synchronized String getValueFromJsonString(String source,
			String key) {
		String value = null;
		JsonNode rootNode = null;
		ObjectMapper mapper = new ObjectMapper();
		try {
			rootNode = mapper.readTree(source);
			value = rootNode.path(key).getTextValue();
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return value;
	}

	/*
	 * private static void setDataFormat2JAVA() { // 设定日期转换格式
	 * JSONUtils.getMorpherRegistry().registerMorpher( new DateMorpher(new
	 * String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" })); }
	 */

	/*
	 * public static void main(String[] arg) throws Exception { String s =
	 * "{\"id\": \"123\",\"name\":\"gumei\",\"age\":\"14\"}"; User user = (User)
	 * getDTO(s,User.class); System.out.println(user.getName()); }
	 */
	public static String getJsonNode(String source, String key) {
		String value = null;
		JsonNode rootNode = null;
		ObjectMapper mapper = new ObjectMapper();
		try {
			rootNode = mapper.readTree(source);
			value = rootNode.path(key).toString();
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return value;
	}

}
